package com.wedo.user.controller;

import com.wedo.auth.pojo.UserInfo;
import com.wedo.auth.utils.JwtUtils;
import com.wedo.common.pojo.PageResult;
import com.wedo.user.config.JwtProperties;
import com.wedo.user.pojo.RequestUser;
import com.wedo.user.pojo.User;
import com.wedo.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("info")
public class UserInfoController {

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private UserService userService;

    @PostMapping
    public ResponseEntity<User> queryUserInfo(@RequestHeader("WD_TOKEN") String token) {
        if (StringUtils.isBlank(token)) {
            return ResponseEntity.badRequest().build();
        }

        User user = new User();
        try {
            UserInfo userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
            user.setId(userinfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        user = this.userService.queryByUserId(user.getId());
        if (user == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(user);

    }

    @PostMapping("uphoto")
    public ResponseEntity<User> updatePhoto(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody User user
    ) {
        if (StringUtils.isBlank(user.getuPhoto()) || StringUtils.isBlank(token)) {
            return ResponseEntity.badRequest().build();
        }
        try {
            UserInfo userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
            user.setId(userinfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        this.userService.updatePhoto(user);
        return ResponseEntity.ok(user);
    }

    @PostMapping("nickname")
    public ResponseEntity<User> updateNickname(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody User user
    ) {
        if (StringUtils.isBlank(user.getNickname()) || StringUtils.isBlank(token)) {
            return ResponseEntity.badRequest().build();
        }

        if (!user.getNickname().matches("^[\\u4E00-\\u9FA5A-Za-z0-9_]+$") || user.getNickname().length() > 10) {
            return ResponseEntity.badRequest().build();
        }

        try {
            UserInfo userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
            user.setId(userinfo.getId());
            this.userService.updateNickname(user);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        return ResponseEntity.ok(user);
    }

    @PostMapping("phone")
    public ResponseEntity<User> updatePhone(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody RequestUser requestUser
    ) {
        if (StringUtils.isBlank(requestUser.getCode()) || StringUtils.isBlank(requestUser.getPhone()) || StringUtils.isBlank(token)) {
            return ResponseEntity.badRequest().build();
        }
        UserInfo userinfo = null;
        // 解析jwt
        try {
            userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }

        requestUser.setId(userinfo.getId());
        User user = this.userService.updatePhone(requestUser);
        return ResponseEntity.ok(user);
    }

    @PostMapping("password")
    public ResponseEntity<User> updatePassword(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody User user
    ) {
        if (StringUtils.isBlank(user.getPassword()) || StringUtils.isBlank(token)) {
            return ResponseEntity.badRequest().build();
        }
        UserInfo userinfo = null;
        // 解析jwt
        try {
            userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }

        user.setId(userinfo.getId());
        User result = this.userService.updatePassword(user);
        return ResponseEntity.ok(result);
    }

    @GetMapping("page")
    public ResponseEntity<PageResult<User>> queryUsersByPage(
            @RequestParam(value = "username",required = false) String username,
            @RequestParam(value = "phone",required = false) String phone,
            @RequestParam(value = "nickname",required = false) String nickname,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        PageResult<User> result = this.userService.queryUsersByPage(username,phone,nickname,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> cancelUser(@RequestHeader("WD_TOKEN")String token,@RequestBody User user) {
        UserInfo userinfo = null;
        // 解析jwt
        try {
            userinfo = JwtUtils.getInfoFromToken(token, this.jwtProperties.getPublicKey());
            if ("ADMIN".equals(userinfo.getRole())) {
                if (user.getId() == null) {
                    return ResponseEntity.badRequest().build();
                }
                Boolean result = this.userService.deleteUser(user.getId());
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        Boolean result = this.userService.deleteUser(userinfo.getId());
        return ResponseEntity.ok(result);
    }

}
